/*
*   jSlider version 1.2
*   Simple Content Slider 
*   
*   Author: Rezaur Rahman 
*   Author Url: http://phpknight.com
* 
*   Usage:
*  
*   $('.classname').jSlider({
*   
*   
*            nextClass:'next',
*            prevClass:'prev',
*            container:'dl',
*            item:'dd',
*            showItems:4,
*            slideSpeed:1000,
*            circular:true
*            click:function(){}
*            hover:function(){}
*        
*    });
*   
* 
* 
*/



(function($)
{                    
    $.fn.jSlider = function(options)
    {
        $.fn.jSlider.defaults = 
        {
            nextClass:'next',
            prevClass:'prev',
            container:'dl',
            item:'dd',
            showItems:4,
            slideSpeed:1000,
            circular:true,
            animation:'swing',
            click:null,
            afterSlide:null  ,
            itemEvent:null,
            transitionSpeed:null,
            viewPortObj:null,
            show:null
        };
                
        var opts = $.extend({}, $.fn.jSlider.defaults, options); /* PUTS ALL THE DEFAULT VALUES INTO A VARIABLE */
        
        
            
        if(!$(this).length)return;
        
        var mainCont=$(this);
        // margin calculation
        var marginLeft=parseInt($(this).find(opts.item).css('marginLeft').replace('px',''));
        var marginRight=parseInt($(this).find(opts.item).css('marginRight').replace('px',''));
        // padding calculation
        var paddingLeft=parseInt($(this).find(opts.item).css('paddingLeft').replace('px',''));
        var paddingRight=parseInt($(this).find(opts.item).css('paddingRight').replace('px',''));
        // border calculation
        var borderLeft=parseInt($(this).find(opts.item).css('borderLeftWidth').replace('px',''));
        var borderRight=parseInt($(this).find(opts.item).css('borderRightWidth').replace('px',''));
        var itemWidth=$(this).find(opts.item).width()+(marginLeft?marginLeft:0)+(marginRight?marginRight:0)+(paddingLeft?paddingLeft:0)+(paddingRight?paddingRight:0)+(borderLeft?borderLeft:0)+(borderRight?borderRight:0);
        var stripWidth;
        var debug=false;
        
        var sliderPos;
        var sliderCont=$(this).find(opts.container);
        
        
        
        
        function d(obj){
            if(!debug)return;
            try {console.log(obj)}
            catch (e){alert(obj)}
        }
        
        // force show an item
        if(opts.show!=null){
            
            // setting the slider position
            
            sliderPos=(opts.show.index()*-itemWidth);
            
            sliderCont.animate({left:sliderPos+'px'},opts.slideSpeed,opts.animation)
            
            
        }
        
        // regular options
        else{
            
        mainCont.find(opts.item).each(function(){
            $(this).html('<div class="jslider_item">'+$(this).html()+'</div>')
        })
        
        if($(this).find(opts.item).length>opts.showItems){
            
            //console.log($(this).find(opts.container).height());
            $(this).css({
                height:$(this).find(opts.container).height()?$(this).find(opts.container).height():$(this).find(opts.item).height(),
                width:(opts.showItems*itemWidth)-marginRight,
                position:'relative'
            })
            
            stripWidth=($(this).find(opts.item).length*itemWidth);
            
            
            
            $(this).find(opts.container).css({
                width:stripWidth,
                position:'absolute',
                top:'0',
                left:'0'
            })
            
            
            var slideLock=false;
            
            // getting the slider initial positions
            sliderPos=sliderCont.position();
            sliderPos=sliderPos.left;
            
            $('.'+opts.nextClass).click(function(e){
                e.preventDefault();
                // deprecated or IE8
                //var pos=sliderCont.position();
                
                d(sliderPos)
                if(!slideLock){
                    if(sliderPos>-(mainCont.find(opts.item).length-opts.showItems)*itemWidth){
                        slideLock=true;    
                        //d(pos.left-itemWidth)
                        sliderPos-=itemWidth;
                        sliderCont.animate({left:sliderPos+'px'},opts.slideSpeed,opts.animation,function(){
                            slideLock=false;
                            d($(this).css('left'))
                            if (opts.afterSlide instanceof Function) { opts.afterSlide.call(); }     
                        });
                        
                    }
                    else if(opts.circular){
                        slideLock=true;
                        var swap=mainCont.find(opts.item+':first').html();
                        mainCont.find(opts.item+':first').remove()
                        
                       
                        
                        mainCont.find(opts.container).append('<'+opts.item+'>'+swap+'</'+opts.item+'>');
                        sliderPos+=itemWidth;
                        mainCont.find(opts.container).css({left:sliderPos+'px'})
                        
                        
                        
                        sliderPos-=itemWidth; 
                        sliderCont.animate({left:sliderPos+'px'},opts.slideSpeed,opts.animation,function(){
                            slideLock=false;
                            d($(this).css('left'))
                            if (opts.afterSlide instanceof Function) { opts.afterSlide.call(); }     
                        });
                    }
                }
              if (opts.click instanceof Function) { opts.click.call(); }     
              if (opts.itemEvent != null) { 
                    
                    bindEvent();
              }  
                
              
            })
            
            $('.'+opts.prevClass).click(function(e){
                e.preventDefault();
                
                if(!slideLock){
                    if(sliderPos<0){
                        slideLock=true;
                        sliderPos+=itemWidth;
                        sliderCont.animate({left:sliderPos+'px'},opts.slideSpeed,opts.animation,function(){
                            slideLock=false;
                            if (opts.afterSlide instanceof Function) { opts.afterSlide.call(); }     
                        });
                    }
                    else if(opts.circular){
                        slideLock=true;
                        var swap=mainCont.find(opts.item+':last').html();
                        mainCont.find(opts.item+':last').remove()
                        mainCont.find(opts.item+':first').before('<'+opts.item+'>'+swap+'</'+opts.item+'>');
                        sliderPos-=itemWidth;
                        mainCont.find(opts.container).css({left:sliderPos+'px'})
                       
                        sliderPos+=itemWidth;
                        sliderCont.animate({left:sliderPos+'px'},opts.slideSpeed,opts.animation,function(){
                            slideLock=false;
                            if (opts.afterSlide instanceof Function) { opts.afterSlide.call(); }     
                        });
                        
                        
                    }
                }
                if (opts.click instanceof Function) { opts.click.call(); }  
                if (opts.itemEvent != null) { 
                    bindEvent();
                }  
                
            })
            
           
           
        }
        else{
            
            $('.'+opts.prevClass).css({visibility:'hidden'})
            $('.'+opts.nextClass).css({visibility:'hidden'})
        }
        
        var j_lock=false;
        
        var callEvent=function(thisobj){
            if(opts.itemEvent=="click"){
                   e.preventDefault();
               }
               
               mainCont.find(opts.item).find('.jslider_item').removeClass('activeslide')
               $(thisobj).find('.jslider_item').addClass('activeslide')
               if(!j_lock){
                   
                   j_lock=true;
                   var thisItem=$(thisobj).find('.jslider_item').attr('rel');
                   if(opts.viewPortObj.find('span:eq('+thisItem+')').attr('rel')!='current'){
                       
                       opts.viewPortObj.find('span[rel=current]').fadeOut(opts.transitionSpeed,function(){
                           $(this).removeAttr('rel')
                           opts.viewPortObj.find('span:eq('+thisItem+')').fadeIn(opts.transitionSpeed,function(){
                               j_lock=false;
                               $(this).attr('rel','current');
                           })
                       })
                   }
                   else j_lock=false;   
                   
                   
               }
        }
        
        function bindEvent(){
            $(document).ready(function(){
                mainCont.find(opts.item).unbind(opts.itemEvent);
                mainCont.find(opts.item).each(function(){
               
                   $(this).bind(opts.itemEvent,function(e){
                      callEvent(this)
                      
                   }).mousemove(function(){
                       if(opts.itemEvent!="click" || opts.itemEvent!="dblclick"){
                           if(!j_lock){
                               
                               
                               j_lock=true;
                               var thisItem=$(this).find('.jslider_item').attr('rel');
                               if(opts.viewPortObj.find('span:eq('+thisItem+')').attr('rel')!='current'){
                                    mainCont.find(opts.item).find('.jslider_item').removeClass('activeslide')
                                    $(this).find('.jslider_item').addClass('activeslide')
                                   
                                   opts.viewPortObj.find('span[rel=current]').fadeOut(opts.transitionSpeed,function(){
                                       $(this).removeAttr('rel')
                                       opts.viewPortObj.find('span:eq('+thisItem+')').fadeIn(opts.transitionSpeed,function(){
                                           j_lock=false;
                                           $(this).attr('rel','current');
                                       })
                                   })
                               }
                               else j_lock=false;   
                               
                               
                           }
                           
                           
                           
                       }
                   })
               })
            })
       }
       // initialize the jGallery
       if (opts.itemEvent != null || opts.autoTransition) { 
            if(opts.itemEvent != null){
                opts.viewPortObj.find('span').hide()
                opts.viewPortObj.find('span:first').show().attr('rel','current')
            }
            mainCont.find(opts.item).eq(opts.showItems-1).find('.jslider_item').addClass('activeslide')
            var i=0;
            mainCont.find(opts.item).each(function(){
                $(this).find('.jslider_item').attr('rel',i++);
            })
            
            if(opts.itemEvent != null){
                bindEvent();
            }
            
       }
       
       
       
       j_mouseover=false;
       
       // auto transiton effect
       
       var autoslide=function(){

           if(!j_mouseover && !slideLock){
               
               var cur_active_item=mainCont.find(opts.item).find('.activeslide').parents(opts.item);
               
               var c=cur_active_item.offset();
               var p=mainCont.offset()
               
               d((p.left+(itemWidth*(opts.showItems-1))))
               d(c.left)
               
               if(c.left>=p.left+(itemWidth*(opts.showItems-1))){
                    $('.'+opts.nextClass).trigger('click');
               }
               
               // if there is an extra viewport for gallery
               if(opts.viewPortObj!=null)
                   cur_active_item.next().each(function(){
                        callEvent(this)
                   });
               else {
                   if(cur_active_item.next().length){
                       cur_active_item.find('.jslider_item').removeClass('activeslide')
                       cur_active_item.next().find('.jslider_item').addClass('activeslide')
                   }
               }
           }
       }
       
       if(opts.autoTransition){
           // pause
           mainCont.mouseenter(function(e){
               
               j_mouseover=true;
           }).mouseleave(function(e){
               j_mouseover=false;
           })
           
           setInterval(autoslide,opts.autoTransition);
           
       }
    }
        
        
    };
})(jQuery);

 


 
 /*
*   jGallery version 1.0
*   Sliding gallery using jSlider
*   this is an extension of jslider
*   
*   Author: Rezaur Rahman 
*   Author Url: http://phpknight.com
* 
*/


(function($)
{                    
    $.fn.jGallery = function(options,extra)
    {
        $.fn.jGallery.defaults = 
        {
            sliderContainer:'.featuredPrograms',
            nextClass:'featuredProgramsNext',
            prevClass:'featuredProgramsPrev',
            showItems:3,
            slideSpeed:250,
            transitionSpeed:500,
            transitionEvent:'click',
            autoTransition:false,
            pauseOnHover:true
            
        };
        
        if(!(extra instanceof Object)){
            extra={};
        }
                
        var opts = $.extend({}, $.fn.jGallery.defaults, options); 
        
        var viewport=$(this);
        
        var slideroptions = $.extend({}, {
                    nextClass:opts.nextClass,
                    prevClass:opts.prevClass,
                    showItems:opts.showItems,
                    slideSpeed:opts.slideSpeed,
                    itemEvent:opts.transitionEvent,
                    viewPortObj:viewport,
                    transitionSpeed:opts.transitionSpeed,
                    circular:opts.autoTransition,
                    autoTransition:opts.autoTransition
                    
            }, extra); 
               
        $(document).ready(function(){
            $(opts.sliderContainer).jSlider(slideroptions);
        })
         
        
        
    };
})(jQuery);


